<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>广告运营管理</el-breadcrumb-item>
        <el-breadcrumb-item>广告解锁功能</el-breadcrumb-item>
        <el-breadcrumb-item>开始展示广告章节配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="menuLeft">
          <span style="color: red">
            配置时，请同时配置同一个版本的，阅读器广告页数与开始展示广告章节
          </span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  getadReadList,
  addReadAd,
  updateAdById,
  deleteAd,
} from "@/api/cat/readad.js";
import { catUrl } from "@/config/env";
import { selectVersion } from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      //数据存储区
      page: {
        total: 1,
      },
      form: {},
      type: true,
      data: [],
      option: {
        index: true,
        indexLabel: "序号",
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: true,
        border: true,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "input",
            search: true,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "广告开始章节数",
            prop: "show_num",
            type: "input",
            required: true,
            rules: [
              {
                required: true,
                message: "广告开始章节数",
              },
            ],
          },
          {
            label: "1",
            prop: "type_name",
            type: "input",
            value: "read_start_show_ad",
            hide: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      delete row["$app_version"];
      row.app_id = "61096114";
      let { data } = await addReadAd(row);

      if (data.code == 1) {
        this.getList();
      } else {
        loading();
        return;
      }
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let { data } = await updateAdById(row);
      if (data.code == 1) {
        this.getList();
      } else {
        loading();
        return;
      }
      done();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let data = await deleteAd(row.id);
        })
        .then(() => {
          this.getList();
        });
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //封装参数
      let param = Object.assign(
        {
          pageNum: this.page.currentPage,
          pageSize: this.page.pageSize,
          type_name: "read_start_show_ad",
        },
        this.params
      );
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await getadReadList(param);
      //修改分页内容
      this.pages(response.data.source);
      //数据赋值
      let list = response.data.source.records;
      this.data = list;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    pages(data) {
      this.page.total = data.total;
      this.page.pageSize = data.size;
      this.page.currentPage = data.current;
    },
    searchChange(params, done) {
      this.condition = [];
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>